package com.office.shop.by.widget;
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.app.Activity;
import android.app.AlertDialog;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import com.office.shop.by.components.CAccounts;
import com.office.shop.by.components.CConfig;
import com.office.shop.by.R;

import java.util.ArrayList;

// Need the following import to get access to the app resources, since this
// class is in a sub-package.

/**
 * The configuration screen for the WidgetProvider widget sample.
 */
public class WidgetConfigure extends Activity {
    static final String TAG = "WidgetConfigure";
    private static final String PREFS_NAME = CConfig.mainPackage+".widget.WidgetProvider";
    private static final String PREF_PREFIX_KEY = CConfig.widgetPrefix;
    private static final String PREF_PREFIX_THEME = CConfig.widgetPrefix + "_theme_";

    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    EditText mAppWidgetPrefix;

    public WidgetConfigure() {
        super();
    }

    @Override
   public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setResult(RESULT_CANCELED);
        setContentView(R.layout.widget_configure);
        setTitle(R.string.app_widget_configure);

        CAccounts.init(WidgetConfigure.this);
        initWidgetConfigure(WidgetConfigure.this);

        if (getIntent().getExtras() != null)
            mAppWidgetId = getIntent().getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID)
            finish();
    }

   public void initWidgetConfigure(final Context context)
   {
        ListView lvWidgetConfigure = (ListView)findViewById(R.id.lvWidgetConfigure);
        lvWidgetConfigure.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , getResources().getStringArray(R.array.widget_configure)));
        lvWidgetConfigure.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        lvWidgetConfigure.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
            switch(i)
            {
                case 0:
                    accountDialog(context);
                    break;
                case 1:
                    themeDialog(context);
                    break;
                default:apply(context);

            }
        }

    });
   }

   public void accountDialog(final Context context)
   {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      int select =  WidgetConfigure.load(context, mAppWidgetId);
      builder.setSingleChoiceItems(CAccounts.logins, select, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                     WidgetConfigure.save(context,mAppWidgetId,item);
                     dialog.dismiss();
            }
      });
      AlertDialog alert = builder.create();
      alert.show();
   }

   public void themeDialog(final Context context)
   {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      int select =  WidgetConfigure.loadTheme(context, mAppWidgetId);
      builder.setSingleChoiceItems(getResources().getStringArray(R.array.widget_themes), select, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                     WidgetConfigure.saveTheme(context,mAppWidgetId,item);
                     dialog.dismiss();
            }
      });
      AlertDialog alert = builder.create();
      alert.show();
   }

   private void apply(Context context)  {
      AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
      WidgetProvider.updateAppWidget(context, appWidgetManager,mAppWidgetId);
      Intent resultValue = new Intent();
      resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
      setResult(RESULT_OK, resultValue);
      finish();
   }

   public static void saveTheme(Context context, int appWidgetId, int themeId)
   {
       context.getSharedPreferences(PREFS_NAME, 0).edit().putInt(PREF_PREFIX_THEME + appWidgetId,themeId).commit();
   }

   public static int loadTheme(Context context, int appWidgetId)
   {
        return context.getSharedPreferences(PREFS_NAME, 0).getInt(PREF_PREFIX_THEME + appWidgetId, 0);
   }

   public static void save(Context context, int appWidgetId, int accountId)
   {
       context.getSharedPreferences(PREFS_NAME, 0).edit().putInt(PREF_PREFIX_KEY + appWidgetId, accountId).commit();
   }

   public static int load(Context context, int appWidgetId)
   {
        return context.getSharedPreferences(PREFS_NAME, 0).getInt(PREF_PREFIX_KEY + appWidgetId, 0);
   }

   public static void delete(Context context, int appWidgetId) { }

   public static void loadAll(Context context, ArrayList<Integer> appWidgetIds, ArrayList<String> texts)  {

   }

}



